package com.biaobiaoqi.scoreanalysis.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import com.biaobiaoqi.scoreanalysis.exception.DuplicateClazzException;
import com.biaobiaoqi.scoreanalysis.exception.NoSuchClazzFoundException;
import com.biaobiaoqi.scoreanalysis.exception.NoSuchTeacherFoundException;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.Clazz;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.ClazzType;
import com.biaobiaoqi.scoreanalysis.service.IClazzService;
import com.biaobiaoqi.scoreanalysis.service.IClazzTypeService;
import com.biaobiaoqi.scoreanalysis.service.ITeacherService;
import com.biaobiaoqi.scoreanalysis.util.SchoolType;

public class ClazzServiceImpl extends DaoServiceImpl<Clazz> implements
		IClazzService {
	@Resource
	ITeacherService teacherService;
	@Resource
	IClazzTypeService clazzTypeService;

	@Override
	public void addClazz(int clazzNumber, int beginYear, int clazzTypeId,
			SchoolType schoolType, String teacherName)
			throws DuplicateClazzException, NoSuchTeacherFoundException {
		if (findClazz(clazzNumber, beginYear) != null) {
			throw new DuplicateClazzException();
		}
		Clazz clazz = new Clazz();
		clazz.setBeginYear(beginYear);
		clazz.setClassNumber(clazzNumber);
		clazz.setClazzType(clazzTypeService.findClazzType(clazzTypeId));
		clazz.setSchoolType(schoolType);
		clazz.setTeacherMaster(teacherService.findTeacher(teacherName));
		getDao().create(clazz);
	}

	@Override
	public void addClazz(int clazzNumber, int beginYear, int clazzTypeId,
			SchoolType schoolType) {
		Clazz clazz = new Clazz();
		clazz.setBeginYear(beginYear);
		clazz.setClassNumber(clazzNumber);
		clazz.setSchoolType(schoolType);
		clazz.setClazzType(clazzTypeService.findClazzType(clazzTypeId));
		getDao().create(clazz);
	}

	@Override
	public void modifyClazz(int clazzNumber, int beginYear,
			ClazzType clazzType, SchoolType schoolType, String teacherName)
			throws NoSuchClazzFoundException, NoSuchTeacherFoundException {
		Clazz clazz = findClazz(clazzNumber, beginYear);
		if (clazz == null) {
			throw new NoSuchClazzFoundException();
		}
		clazz.setClazzType(clazzType);
		clazz.setSchoolType(schoolType);
		clazz.setTeacherMaster(teacherService.findTeacher(teacherName));
		getDao().save(clazz);
	}

	@Override
	public void deleteClazz(int clazzNumber, int beginYear)
			throws NoSuchClazzFoundException {

		Clazz clazz = findClazz(clazzNumber, beginYear);
		if (clazz != null) {
			getDao().delete(clazz);
		} else {
			throw new NoSuchClazzFoundException();
		}
	}

	@Override
	public Clazz findClazz(int clazzId) {
		return getDao().findById(Clazz.class, clazzId);
	}

	@Override
	public Clazz findClazz(int clazzNumber, int beginYear) {
		Clazz clazz = getDao().queryUnique(
				"select from Clazz c where c.clazzNumber=? and c.beginYear=?",
				"" + clazzNumber, beginYear);
		return clazz;
	}

	@Override
	public List<Clazz> findClazzByYear(int beginYear) {

		List<Clazz> list = getDao().queryList(
				"select from Clazz c where c.beginYear=?", beginYear);
		return list;
	}

	public ITeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	@Override
	public List<Clazz> listAllClazz(int begin, int number) {
		return getDao().queryList("from Clazz c order by c.beginYear desc, c.classNumber",
				begin, number);
	}

	@Override
	public void deleteClazz(int clazzId) {
		getDao().delete(findClazz(clazzId));
	}
}
